package com.wizz.trade.information.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wizz.trade.information.dto.page.VarietyPageDto;
import com.wizz.trade.information.entity.Variety;
import com.wizz.trade.information.mapper.VarietyMapper;
import com.wizz.trade.information.service.VarietyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wizz.trade.common.PageVo;
import com.wizz.trade.information.vo.VarietyAllNameVo;
import com.wizz.trade.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 品种表 服务实现类
 * </p>
 *
 * @author fyftless
 * @since 2023-04-02 04:26:06
 */
@Service
public class VarietyServiceImpl extends ServiceImpl<VarietyMapper, Variety> implements VarietyService {

    @Resource
    private VarietyMapper varietyMapper;

    @Override
    public void addVariety(Variety variety) {
        varietyMapper.insert(variety);
    }

    @Override
    public void updateVariety(Variety variety) {
        varietyMapper.updateById(variety);
    }

    @Override
    public void deleteVariety(Integer id) {
        varietyMapper.deleteById(id);
    }

    @Override
    public PageVo<Variety> getPage(VarietyPageDto varietyPageDto) {
        LambdaQueryWrapper<Variety> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(varietyPageDto.getName()),Variety::getName, varietyPageDto.getName());
        IPage<Variety> page = varietyMapper.selectPage(PageUtil.initPage(varietyPageDto), queryWrapper);
        return new PageVo<>(page);
    }

    @Override
    public List<Variety> getAll() {
        return varietyMapper.selectList(null);
    }

    @Override
    public List<VarietyAllNameVo> getAllName() {
        List<Variety> raw = varietyMapper.getAllName();
        List<VarietyAllNameVo> res = new ArrayList<>();
        for (Variety variety : raw) {
            VarietyAllNameVo varietyAllNameVo = new VarietyAllNameVo();
            BeanUtils.copyProperties(variety, varietyAllNameVo);
            res.add(varietyAllNameVo);
        }
        return res;
    }

    @Override
    public String getNameById(Integer variety) {
        return varietyMapper.getNameById(variety);
    }

    @Override
    public List<Map<Integer, String>> getNames() {
        return varietyMapper.getNames();
    }

    @Override
    public List<Map<Integer, String>> getOptions() {
        return varietyMapper.getOptions();
    }


}
